package org.sang.redpackets;

import android.accessibilityservice.AccessibilityService;
import android.annotation.TargetApi;
import android.app.Notification;
import android.content.Intent;
import android.os.Build;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.Toast;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 类描述:微信红包6.6.0版本聊天界面抢红包助手
 * 作者:${LiangAn}
 * 时间:2017年12月29日 14:18:05
 * 备注:
 */

public class RedPacketsAccessibilityService extends AccessibilityService {

    private Set<Integer> currentScreenRedPackets = new HashSet<>();
    private int lastFirstVisiableRedPackets;

    @Override
    public void onCreate() {
        super.onCreate();
        Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher).setContentTitle("红包助手运行中");
        startForeground(1, builder.build());
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        if (event.getSource() != null) {
            AccessibilityNodeInfo rootInActiveWindow = getRootInActiveWindow();
            if (rootInActiveWindow != null) {
                if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SCROLLED) {
                    //抢红包
                    List<AccessibilityNodeInfo> list = rootInActiveWindow.findAccessibilityNodeInfosByText("领取红包");
                    if (list != null && list.size() > 0) {
                        AccessibilityNodeInfo firstNode = list.get(0);
                        if (lastFirstVisiableRedPackets != firstNode.hashCode()) {
                            currentScreenRedPackets.remove(lastFirstVisiableRedPackets);
                            lastFirstVisiableRedPackets = firstNode.hashCode();
                        }
                        for (int i = list.size() - 1; i > -1; i--) {
                            AccessibilityNodeInfo nodeInfo = list.get(i);
                            if (!currentScreenRedPackets.contains(nodeInfo.hashCode())) {
                                AccessibilityNodeInfo parent = nodeInfo.getParent();
                                if (parent != null) {
                                    parent.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                                    currentScreenRedPackets.add(nodeInfo.hashCode());
                                    break;
                                }
                            }
                        }

                    }
                }
                //拆红包，这个方法是获取到微信上的id控件名称 com.tencent.mm:id/c2i
                List<AccessibilityNodeInfo> nodeInfoList = rootInActiveWindow.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/c4j");
                if (nodeInfoList != null && nodeInfoList.size() > 0) {
                    for (AccessibilityNodeInfo accessibilityNodeInfo : nodeInfoList) {
                        accessibilityNodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                    }
                }
                //第二中方法、没有上面的精确
                //openRedPacket();
            }
        }
    }

    /**
     * 这个方法是遍历按钮，但是如果多了效率没有那么高.
     */
    private void openRedPacket() {
        AccessibilityNodeInfo rootNode = getRootInActiveWindow();
        for (int i = 0; i < rootNode.getChildCount(); i++) {
            AccessibilityNodeInfo node = rootNode.getChild(i);
            if ("android.widget.Button".equals(node.getClassName())) {
                node.performAction(AccessibilityNodeInfo.ACTION_CLICK);
            }
            openRedPacket();
        }
    }


    /**
     * 服务连接
     */
    @Override
    protected void onServiceConnected() {
        Log.e("红包service","抢红包服务开启");
        super.onServiceConnected();
    }

    /**
     * 必须重写的方法：系统要中断此service返回的响应时会调用。在整个生命周期会被调用多次。
     */
    @Override
    public void onInterrupt() {
        Log.e("红包service","我快被终结了啊-----");
    }

    /**
     * 服务断开
     */
    @Override
    public boolean onUnbind(Intent intent) {
        Log.e("红包service","抢红包服务已被关闭");
        return super.onUnbind(intent);
    }

}
